package teamgodeater.hicarnet.MVP.Ui.Main;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.model.LatLng;

import butterknife.Bind;
import butterknife.ButterKnife;
import teamgodeater.hicarnet.Data.UserPointData;
import teamgodeater.hicarnet.Help.RestClientHelp;
import teamgodeater.hicarnet.Help.UserDataHelp;
import teamgodeater.hicarnet.R;
import teamgodeater.hicarnet.RestClient.RestClient;
import teamgodeater.hicarnet.Utils.Utils;
import teamgodeater.hicarnet.Widget.RippleBackGroundView;

import static teamgodeater.hicarnet.Activity.ManageActivity.manageActivity;
import static teamgodeater.hicarnet.Help.UserDataHelp.userPointData;

/**
 * Created by G on 2016/7/22 0022.
 */

public class TrafficPointPresent extends MainContractor.TrafficPointPresent {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.back)
    RippleBackGroundView back;
    @Bind(R.id.searchLoc)
    RippleBackGroundView searchLoc;
    @Bind(R.id.del)
    RippleBackGroundView del;
    @Bind(R.id.save)
    RippleBackGroundView save;
    @Bind(R.id.tipIco)
    View tipIco;
    @Bind(R.id.tip)
    TextView tip;

    private boolean isStart = false;
    private boolean isAdd = false;
    private View trafficView;

    private MainPresent mDspPresenter;
    private ImageView gMarker;


    @Override
    public void showTrafficPointSet() {
        mView.viewPagerFramelayout.addView(trafficView);
        int width = mView.viewPagerFramelayout.getWidth();
        int height = mView.viewPagerFramelayout.getHeight();
        float maxR = (float) Math.sqrt(width * width + height * height);
        Animator circularReveal = ViewAnimationUtils.createCircularReveal(trafficView, width - 60
                , 0, 0, maxR);
        circularReveal.setInterpolator(new AccelerateInterpolator());
        circularReveal.start();
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                showMarker();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                showMarker();
            }
        });
    }

    @Override
    public void hideTrafficPointSet() {
        int width = mView.viewPagerFramelayout.getWidth();
        int height = mView.viewPagerFramelayout.getHeight();
        float maxR = (float) Math.sqrt(width * width + height * height);
        Animator circularReveal = ViewAnimationUtils.createCircularReveal(trafficView, width - 60
                , 0, maxR, 0);
        circularReveal.setInterpolator(new AccelerateInterpolator());
        circularReveal.start();
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mView.viewPagerFramelayout.removeView(trafficView);
                removeMarket();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                mView.viewPagerFramelayout.removeView(trafficView);
                removeMarket();
            }
        });
    }

    public void inflater() {
        if (trafficView != null)
            return;
        LayoutInflater from = LayoutInflater.from(mView.viewPagerFramelayout.getContext());
        trafficView = from.inflate(R.layout.frgm_main_bottom_traffic, mView.viewPagerFramelayout, false);
        ViewGroup.LayoutParams layoutParams = trafficView.getLayoutParams();
        layoutParams.height = mView.viewPagerFramelayout.getHeight();
        layoutParams.width = mView.viewPagerFramelayout.getWidth();
        trafficView.setLayoutParams(layoutParams);
        ButterKnife.bind(this, trafficView);
        setListener();
    }

    private void setListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideTrafficPointSet();
                if (isAdd) {
                    reset();
                    return;
                }
                showFab();
                mDspPresenter.mapSearchPresent.showBeforeRoute();
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new RestClientHelp().setUserPoint(userPointData, new RestClient.OnResultListener<String>() {
                    @Override
                    public void succeed(String bean) {
                        UserDataHelp.getUserPointData(new UserDataHelp.OnDoneListener() {
                            @Override
                            public void onDone(int code) {
                                mDspPresenter.overViewPresent.addToPager();
                                Utils.toast("添加成功! 本路线发生拥堵将会提醒!");
                            }
                        });
                    }
                    @Override
                    public void error(int code) {
                        RestClientHelp.generalErrorToast(code);
                        Utils.toast("添加交通提醒点失败!!");
                    }
                });
            }
        });
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        searchLoc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDspPresenter.mapSearchPresent.addTraffic();
            }
        });
    }

    public void showFab() {
        if (isStart)
            mView.showActionFab(R.drawable.ic_create);
    }

    public void hideFab() {
        mView.hideActionFab();
    }

    public void showMarker() {
        removeMarket();
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromAssetWithDpi("loc_market.png");
        gMarker = new ImageView(mView.getContext());
        Bitmap bitmap = bitmapDescriptor.getBitmap();
        gMarker.setImageBitmap(bitmap);
        mView.mTopView.addView(gMarker,1,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        Point mapCenterPoint = mDspPresenter.mapPresent.getMapCenterPoint();
        gMarker.setX(mapCenterPoint.x - bitmap.getWidth() / 2.5f);
        gMarker.setY(mapCenterPoint.y - bitmap.getHeight() * 0.8f);
        gMarker.setScaleX(0.8f);
        gMarker.setScaleY(0.8f);
        setMapCenterToTrafficPoint();
        mDspPresenter.mapPresent.setOnMapTouchListener(new MapPresent.OnGMapTouchListener() {
            @Override
            public void OnTouch(int action) {
                if (gMarker == null)
                    return;
                if (action == MotionEvent.ACTION_DOWN) {
                    gMarker.animate().scaleY(1f).scaleX(1f).setDuration(150L).start();
                } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                    gMarker.animate().scaleY(0.8f).scaleX(0.8f).setDuration(150L).start();
                    MapPresent mapPresent = mDspPresenter.mapPresent;
                    LatLng latLng = mapPresent.baiduMap.getProjection().fromScreenLocation(mapPresent.getMapCenterPoint());
                    UserPointData userPointData = UserDataHelp.userPointData;
                    userPointData.setLatitude(latLng.latitude);
                    userPointData.setLongitude(latLng.longitude);
                }
            }
        });
    }

    public void setgMarkerVisible(boolean visible){
        if (gMarker == null)
            return;
        if (visible)
            gMarker.setVisibility(View.VISIBLE);
        else
            gMarker.setVisibility(View.INVISIBLE);
    }

    public void removeMarket() {
        if (gMarker != null)
            mView.mTopView.removeView(gMarker);
        mDspPresenter.mapPresent.setOnMapTouchListener(null);
        gMarker = null;
    }

    public boolean setMapCenterToTrafficPoint() {
        if (gMarker == null)
            return false;
        LatLng latLng;
        UserPointData userPointData = UserDataHelp.userPointData;
        if (userPointData != null && userPointData.getLatitude() != 0) {
            latLng = new LatLng(userPointData.getLatitude(), userPointData.getLongitude());
        } else {
            latLng = Utils.getLatLng(manageActivity.getMyLocation());
        }
        mDspPresenter.mapPresent.zoomToTarget(latLng, 15f);
        return true;
    }


    public void start(boolean iad) {
        isStart = true;
        isAdd = iad;
        inflater();
        if (isAdd) {
            showTrafficPointSet();
            return;
        }
        showFab();
        mView.actionFab.setOnClickListener(onClickListener);
    }

    public void reset() {
        isStart = false;
        hideFab();
        removeMarket();
        mView.actionFab.setOnClickListener(null);
        ButterKnife.unbind(this);
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hideFab();
            showTrafficPointSet();
            mDspPresenter.mapSearchPresent.hideCurrentRoute();
        }
    };

    @Override
    protected void onStart() {
        mDspPresenter = mView.mPresenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDspPresenter = null;
        ButterKnife.unbind(this);
    }
}
